// pages/orderList/orderList.js
var couponList = [];
function CouponBean() {
  var id;
  var score;
  var title;
  var content;
  var money;
  var image;
}
Page({

  /**
   * 页面的初始数据
   */
  data: {
       isSetInfo: false,
       stateList: [
            "可换优惠券","等待.."
       ],

       common_row: {
            caption_class: 'caption',
            value_class: 'value'
       },

       couponArray:[],
  },


  onLoad: function () {
    couponList = [];
    var that = this;
    this.setData({
      isSetInfo:getApp().globalData.isSetInfo
    })
    wx.request({
      url:"http://101.37.14.167:8001/Coupon/findAll",
      method:"GET",
      data:{

      },
      success:function(res){
          console.log(res)
          console.log(res.data.data.length)
          var list = res.data.data;
          for(var i=0;i<list.length;i++)
          {
            var couponBean = new CouponBean();
            couponBean.id = res.data.data[i].id;
            couponBean.score = res.data.data[i].score;
            couponBean.title = res.data.data[i].title;
            couponBean.content = res.data.data[i].content;
            couponBean.money = res.data.data[i].money;
            couponBean.image='../../res/images/ic_hotel_image.png';
            couponList.push(couponBean);
          }
          that.setData({
            couponArray:couponList
          })
          console.log(that.data.couponArray)
      },
      fail:function(res){
          console.log("失败"+res)
          
      }
  })
  },
 exchange:function(res){
   console.log(res);
   var that = this;
   var id = res.target.dataset.id;
          var score = res.target.dataset.score;
          if(this.data.isSetInfo){
            console.log("现有积分"+getApp().globalData.account.score)
            if(getApp().globalData.account.score>=score)
            {
              console.log("现有积分"+getApp().globalData.account.score)
            }
            wx.request({
              url:"http://101.37.14.167:8001/Coupon/exchange",
              method:"POST",
              data:{
                accountId:getApp().globalData.account.id,
                couponId:id
              },
              success:function(res){
                console.log(res)
              },
              fail:function(res){
                console.log(res)
              }
            })
          }else{
            console.log("积分不足或未登录")
          }
 }
})